package singletontest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 静态代码块中的代码在使用类的时候就已经执行了，所以可以应用静态代码块的这个特点来实现单例设计模式。
 *
 * @author liuziyang
 * @create 2019-10-14 20:50
 */
public class SingletonTest9 {
    private static SingletonTest9 myObject = null;

    static {
        myObject = new SingletonTest9();
    }

    public static SingletonTest9 getInstance() {
        return myObject;
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest9.getInstance().hashCode());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest9.getInstance().hashCode());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest9.getInstance().hashCode());
            }
        }).start();
    }
}
